#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

typedef long long LL;
const int N = 200010;

int n;
LL h[N];

struct Seg
{
    int l, r;
    LL v;
    bool operator< (const Seg& S) const
    {
        if (v != S.v) return v < S.v;
        return l > S.l;
    }
};

LL f(LL x)
{
    return sqrtl(x / 2 + 1);
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%lld", &h[i]);

    priority_queue<Seg> heap;

    for (int i = 0; i < n; i++)
    {
        int j = i + 1;
        while (j < n && h[i] == h[j]) j++;
        heap.push({ i, j - 1, h[i] });
        i = j - 1;
    }

    int res = 0;
    while (heap.size() > 1 || heap.top().v > 1)
    {
        auto t = heap.top();
        heap.pop();

        while (heap.size() && heap.top().v == t.v && t.r + 1 == heap.top().l)
        {
            t.r = heap.top().r;
            heap.pop();
        }
        heap.push({ t.l, t.r, f(t.v) });

        if (t.v > 1) res++;
    }

    printf("%d\n", res);

    return 0;
}